package com.mygame.game_server.util;

import com.mygame.game_server.conf.AllConf;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * redis工具类
 */
public final class RedisUtil {

    final private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
    /**
     * redis连接池
     */
    static private JedisPool _jedispool = null;
    /**
     * 私有化
     */
    private RedisUtil() {

    }

    /**
     * 初始化
     */
    static public void  init() {
        try {
            _jedispool = new JedisPool(AllConf.REDIS_SERVER_HOST,AllConf.REDIS_SERVER_PORT);
//            _jedispool = new JedisPool(new GenericObjectPoolConfig(), "127.0.0.1", 6379, 2000, "root");
            logger.info("redis 连接成功");
        } catch (Exception ex) {
            logger.error(ex.getMessage(),ex);
        }
    }

    /**
     * 获取jedis是例
     * @return
     */
    static  public Jedis getJedis() {
        if (_jedispool == null) {
            throw new RuntimeException("jiedspool 未初始化");
        }

        Jedis jedis = _jedispool.getResource();
        jedis.auth("root");

        return jedis;
    }

}
